﻿<%@ Page Title="" Language="C#" MasterPageFile="~/AdminCP/Layouts/Admin.Master" AutoEventWireup="true"
    CodeBehind="BonusDetailByMonth.aspx.cs" Inherits="MLM.AdminCP.Pages.BonusDetailByMonth"
    meta:resourcekey="PageResource1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Label runat="server" ID="lbBonusdetailbymonth" Text="Bonus detail by month"
        meta:resourcekey="lbBonusdetailbymonthResource1"></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <h3>
        <asp:Label runat="server" ID="lbFilter" Text="Filter" meta:resourcekey="lbFilterResource1"></asp:Label>
    </h3>
    <div>
        <asp:Label runat="server" ID="lbUserID" Text="UserID:" meta:resourcekey="lbUserIDResource1"></asp:Label>
        <telerik:RadTextBox runat="server" ID="UserIDTxt" meta:resourcekey="UserIDTxtResource1"
            Width="125px" />
        <asp:Label runat="server" ID="lbYear" Text=" Year:" meta:resourcekey="lbYearResource1"></asp:Label>
        <telerik:RadComboBox runat="server" ID="FromYearCbb" meta:resourcekey="FromYearCbbResource1" />
        <asp:Label runat="server" ID="lbMonth" Text="Month" meta:resourcekey="lbMonthResource1"></asp:Label>
        <telerik:RadComboBox runat="server" ID="MonthCbb" meta:resourcekey="MonthCbbResource1" />
        <a href="#" class="button" id="btnView">
            <asp:Label runat="server" ID="lbViewbonusdetail" Text="View bonus detail" meta:resourcekey="lbViewbonusdetailResource1"></asp:Label></a><br />
        <br />
    </div>
    <div id="DetailInfo">
    </div>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            String.prototype.format = function () {
                var text = this;
                var tokenCount = arguments.length;
                if (tokenCount < 1) {
                    return text;
                }
                for (var token = 0; token < tokenCount; token++) {
                    text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token]);
                }
                return text;
            };
            function GetOnlyDate(strDate) {
                strDate = strDate.replace("/Date(", "").replace(")/", "");
                var dt = new Date(parseInt(strDate));
                var date = dt.getDate();
                var mon = dt.getMonth() + 1;
                if (date < 10) date = '0' + date;
                if (mon < 10) mon = '0' + mon;
                return "{0}/{1}/{2}".format(date, mon, dt.getFullYear());
            }
            $(function () {
                $("#btnView").click(function (e) {
                    e.stopPropagation();
                    var userId = $find('<%=UserIDTxt.ClientID %>').get_value();
                    if (userId == '') {
                        alert("Please input UserID!");
                        return false;
                    }
                    var year = $find("<%=FromYearCbb.ClientID %>").get_selectedItem().get_value();
                    var month = $find("<%= MonthCbb.ClientID %>").get_selectedItem().get_value();
                    $.ajax({
                        type: "POST",
                        url: "/AdminCP/Pages/BonusDetailByMonth.aspx/GetOwnPv",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        beforeSend: function () {
                            $('body').append('<div id="loadingDiv" style="position:fixed; top:50%; left:50%; width:200px;text-align:center; padding:3px; border:solid 1px #ccc; background:#f1f1f1;">Loading data, please wait...</div>');
                        },
                        data: "{'_year':'" + year + "','_month':'" + month + "','_userId':'" + userId + "'}",
                        async: true,
                        success: function (msj) {
                            var item = JSON.parse(msj.d);
                            var $elm = $('#DetailInfo');
                            $elm.empty();
                            $elm.append('<h3>My Info</h3>');
                            $elm.append('<div id="MyInfoDiv"><span>User Id:</span><b>' + item.UserId + '</b><br /><span>Name</span><b>' + item.Name + '</b><br /><span>Point</span><b>' + item.Point + 'PV</b><br /><span>Bonus</span><b>' + item.Bonus + ' %</b><br /><span>My Selling in &euro;</span><b>' + item.Selling + ' &euro;</b><br /></div>');
                            $elm.append('<div id="MySponsorDiv"><h3>My Recommended User</h3></div>');
                            $.each(item.RecommendUser, function (i, user) {
                                $('#MySponsorDiv').append('<div id="MyInfoDiv"><span>User Id:</span><b>' + user.UserId + '</b><br /><span>Name</span><b>' + user.Name + '</b><br /><span>Point</span><b>' + user.Point + ' PV</b><br /><span>Bonus</span><b>' + user.Bonus + ' %</b><br /><span>Selling in &euro; </span><b>' + user.Selling + ' &euro; </b><br /></div>');
                            });
                            $elm.append('<div id="TotalSellingsDiv"><h3>My Total Earned</h3></div>');
                            var total = 0;
                            var myMoney = Number((item.Bonus * item.Selling / 100).toFixed(2));
                            total += myMoney;
                            $('#TotalSellingsDiv').append('<span class="sub_total_price_label">Discount for own sellings: ' + item.Bonus + '% * ' + item.Selling + ' =</span><span class="sub_total_money_get">' + myMoney + ' &euro;</span><br />');
                            var recommendSaleHeadMoney = 0;
                            $.each(item.RecommendUser, function (i, user) {
                                var userMoney = Number((((item.Bonus - user.Bonus) * user.Selling) / 100).toFixed(2));
                                total += userMoney;
                                if (user.Bonus === 20) {//20% is sale head                                    
                                    recommendSaleHeadMoney += user.Selling;
                                }
                                $('#TotalSellingsDiv').append('<span class="sub_total_price_label">Difference from ' + user.UserId + '(' + user.Name + '): ' + (item.Bonus - user.Bonus) + ' % =</span><span class="sub_total_money_get">' + userMoney + ' &euro;</span><br />');
                            });
                            var carierBonusMoney = 0;
                            if (item.CarrierBonus.NumberOfSalesHead == 1) {
                                carierBonusMoney = item.CarrierBonus.Bonus * item.Selling / 100;
                            } else if (item.CarrierBonus.NumberOfSalesHead == 2) {
                                carierBonusMoney = recommendSaleHeadMoney * (item.CarrierBonus.Bonus / 100) * 50 / 100;
                            } else if (item.CarrierBonus.NumberOfSalesHead == 3) {
                                console.log(recommendSaleHeadMoney);
                                carierBonusMoney = recommendSaleHeadMoney * (item.CarrierBonus.Bonus / 100) * 70 / 100;
                            }
                            else if (item.CarrierBonus.NumberOfSalesHead >= 4) {
                                carierBonusMoney = recommendSaleHeadMoney * (item.CarrierBonus.Bonus / 100); // * (item.CarrierBonus.NumberOfSalesHead / item.CarrierBonus.NumberOfSalesHeadToGetFullBonus);
                            }
                            carierBonusMoney = Number((carierBonusMoney).toFixed(2));
                            if (isNaN(carierBonusMoney)) {
                                carierBonusMoney = 0;
                            }
                            total += carierBonusMoney;
                            $('#TotalSellingsDiv').append('<span class="sub_total_price_label">Carrier Bonus ' + item.CarrierBonus.Bonus + '% (' + item.CarrierBonus.NumberOfSalesHead + ' sales head) = </span><span class="sub_total_money_get">' + carierBonusMoney + ' &euro;</span><br />');
                            $('#TotalSellingsDiv').append('<br /><b>Total: </b><span id="total_marketing_get_on_euro">' + Number((total).toFixed(2)) + ' &euro;</span>');
                        }
                    });
                    return false;
                });
                $(document).ajaxStop(function () {
                    $('#loadingDiv').remove();
                });
            });            
        </script>
    </telerik:RadCodeBlock>
    <style type="text/css">
        #MyInfoDiv { width: 200px; border: solid 1px #ccc; background: #f1f1f1; padding: 5px 10px; float: left; margin: 5px; }
        #MyInfoDiv span { width: 100px; display: inline-block; }
        #MySponsorDiv { display: inline-block; width: 100%; }
        #total_marketing_get_on_euro { font-size: 20px; }
        .sub_total_money_get { display: inline-block; font-weight: bold; text-align: right; width: 140px; }
        .sub_total_price_label { display: inline-block; margin-bottom: 10px; width: 400px; }
    </style>
</asp:Content>
